/*
 * testServidorRemoto.cpp
 *
 *  Created on: 28/07/2014
 *      Author: migue
 */


#include "../defines.h"
#include "../comun/ServidorRemoto.h"
#include <sys/types.h>
#include <unistd.h>

#include <iostream>
#include <map>

using namespace std;

int main(int arch, char** argv) {
	std::map<int,int> ids;

	ServidorRemoto serv;
	int errores = 0;

	int idBroker1 = serv.pedirIdBroker(1, 500);
	cout << "id broker 1 : " << idBroker1 << endl;
	ids[idBroker1] = idBroker1;

	int idBroker2 = serv.pedirIdBroker(4, 500);
	cout << "id broker 2 : " << idBroker2 << endl;
	ids[idBroker2] = idBroker2;

	int idSala2 = serv.pedirIdSala(2, Tipos::TP_ENTRADA_COLA, Tipos::LUGAR_ZOO);
	cout << "id Sala 2 (CZ): " << idSala2 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
	ids[idSala2] = serv.idBrokerConectado();

	int idSala1 = serv.pedirIdSala(1, Tipos::TP_ENTRADA_COLA, Tipos::LUGAR_ENTRADA);
	cout << "id Sala 1 (CE): " << idSala1 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
	int idBrokerSala1 = serv.idBrokerConectado();
	ids[idSala1] = idBrokerSala1;


	int id;
	cout << endl;

	///**************************************************************************************///
	// ENTRADAS a COLAS

	for (int i = 1 ; i <= CANT_ENTRADAS ; ++i) {

		id = serv.pedirIdSala(i, Tipos::TP_ENTRADA_COLA, Tipos::LUGAR_ENTRADA);
		cout << "id Sala "<< i <<" (CE): " << id <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
		ids[id] =  serv.idBrokerConectado();

		id = serv.pedirIdSala(i, Tipos::TP_ENTRADA_COLA, Tipos::LUGAR_ZOO);
		cout << "id Sala "<< i <<" (CZ): " << id <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
		ids[id] =  serv.idBrokerConectado();

	}
	///**************************************************************************************///

	cout << endl;
	///**************************************************************************************///
	// ENTRADAS a SALA

	for (int i = 1 ; i <= CANT_ENTRADAS ; ++i) {

		id = serv.pedirIdSala(i, Tipos::TP_ENTRADA_SALA, Tipos::LUGAR_ENTRADA);
		cout << "id Sala "<< i <<" (EE): " << id <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
		ids[id] =  serv.idBrokerConectado();

		id = serv.pedirIdSala(i, Tipos::TP_ENTRADA_SALA, Tipos::LUGAR_ZOO);
		cout << "id Sala "<< i <<" (EZ): " << id <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
		ids[id] =  serv.idBrokerConectado();


	}
	cout << endl;
	///**************************************************************************************///
	///**************************************************************************************///
	// SALIDAS DE SALA

	for (int i = 1 ; i <= CANT_ENTRADAS ; ++i) {

		id = serv.pedirIdSala(i, Tipos::TP_SALIDA, Tipos::LUGAR_ENTRADA);
		cout << "id Sala "<< i <<" (SE): " << id <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
		ids[id] =  serv.idBrokerConectado();

		id = serv.pedirIdSala(i, Tipos::TP_SALIDA, Tipos::LUGAR_ZOO);
		cout << "id Sala "<< i <<" (SZ): " << id <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
		ids[id] =  serv.idBrokerConectado();;


	}
	///**************************************************************************************///

	cout << endl;


	int idSala3 = serv.pedirIdSala(3, Tipos::TP_ENTRADA_SALA, Tipos::LUGAR_ENTRADA);
	cout << "id Sala 3 (EE): " << idSala3 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
	ids[idSala3] = serv.idBrokerConectado();


	int idSala1_1 = serv.pedirIdSala(1, Tipos::TP_ENTRADA_SALA, Tipos::LUGAR_ENTRADA);
	cout << "id Sala 1 (EE) : " << idSala1_1 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
	int idBrokerSala1_1 = serv.idBrokerConectado();
	ids[idSala1_1] = idBrokerSala1_1;

	int idSala2_1 = serv.pedirIdSala(2, Tipos::TP_ENTRADA_SALA, Tipos::LUGAR_ZOO);
	cout << "id Sala 2_1 (EZ): " << idSala2_1 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
	ids[idSala2_1] = serv.idBrokerConectado();

	int idSala3_1 = serv.pedirIdSala(3, Tipos::TP_SALIDA, Tipos::LUGAR_ENTRADA);
	cout << "id Sala 3_1 (SE): " << idSala3_1 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
	ids[idSala3_1] = serv.idBrokerConectado();


	int idBroker3 = serv.pedirIdBroker(2, 500);
	cout << "id broker 3 : " << idBroker3 << endl;
	ids[idBroker3] = serv.idBrokerConectado();

	cout << endl << endl;

	int idBus1 = serv.pedirIdBus(1);
	cout << "id Bus 1: " << idBus1 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
	ids[idBus1] = serv.idBrokerConectado();

	int idBus2 = serv.pedirIdBus(2);
	cout << "id Bus 2: " << idBus2 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
	ids[idBus2] = serv.idBrokerConectado();

	int idBus3 = serv.pedirIdBus(3);
	cout << "id Bus 3: " << idBus3 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
	ids[idBus3] = serv.idBrokerConectado();

	int idBus4 = serv.pedirIdBus(4);
	cout << "id Bus 4: " << idBus4 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;
	ids[idBus4] = serv.idBrokerConectado();

	int brokerOriginal = serv.idBrokerConectado();

	int idRepuestoGral;

	int idTemp = serv.pedirIdBUSRepuestoGeneral(idRepuestoGral);
	ids[idTemp] = -555; // devolvera este id cuando ya no exista mas
	int broker =serv.idBrokerConectado();

	cout << "Id Temporal para el reemplazo del bus 4: " << idTemp << ". idBroker: "<< broker <<endl;


	int idReemp = serv.pedirIdBUSRepuesto(4, idTemp);

	cout << "Reemplazando BUS" << endl;

	if (idReemp != idBus4) {
		cout << "ERROR ERROR" << endl;
		errores++;
	}

	serv.consultarDatos(idReemp);

	cout << "id Bus 4 (Reemplazo): " << idReemp <<  ". idBroker: "<< serv.idBrokerConectado() <<endl;
	ids[idReemp] = serv.idBrokerConectado();
	if (brokerOriginal == serv.idBrokerConectado()) {
		cout << "ERROR ERROR mal reemplazo de brokers " << endl;
		errores++;
	}



	int idNuevoBroker = serv.informarBrokerCaido(idSala1, idBrokerSala1);
	cout << endl <<"Informando broker " << idBrokerSala1 << " caido. Nuevo broker(id:"<< idNuevoBroker <<") de Sala 1 (id: " << idSala1 <<")." << endl << endl;
	ids[idSala1] = serv.idBrokerConectado();


	int idPer1 = serv.pedirIdPersona(500);
	ids[idPer1] = serv.idBrokerConectado();
	cout << "id Persona(500) 1: " << idPer1 << " idBroker: "<< serv.idBrokerConectado() << endl;

	int idPer2 = serv.pedirIdPersona(501);
	ids[idPer2] = serv.idBrokerConectado();
	cout << "id Persona(501) 2: " << idPer2 << " idBroker: "<< serv.idBrokerConectado() << endl;


	int idPer3 = serv.pedirIdPersona(502);
	ids[idPer3] = serv.idBrokerConectado();
	cout << "id Persona(502) 3: " << idPer3 << " idBroker: "<< serv.idBrokerConectado() << endl;

	int idPer4 = serv.pedirIdPersona(503);
	ids[idPer4] = serv.idBrokerConectado();
	cout << "id Persona(503) 4: " << idPer4 << " idBroker: "<< serv.idBrokerConectado() << endl;


	int idPer5 = serv.pedirIdPersona(504);
	ids[idPer5] = serv.idBrokerConectado();
	cout << "id Persona(504) 5: " << idPer5 << " idBroker: "<< serv.idBrokerConectado() << endl;

	int idPer6 = serv.pedirIdPersona(505);
	ids[idPer6] = serv.idBrokerConectado();
	cout << "id Persona(505) 6: " << idPer6 << " idBroker: "<< serv.idBrokerConectado() << endl;


	int idPer7 = serv.pedirIdPersona(502);
	ids[idPer7] = serv.idBrokerConectado();
	cout << "id Persona(502) 7: " << idPer7 << " idBroker: "<< serv.idBrokerConectado() << endl;

	int idPer8 = serv.pedirIdPersona(503);
	ids[idPer8] = serv.idBrokerConectado();
	cout << "id Persona(503) 8: " << idPer8 << " idBroker: "<< serv.idBrokerConectado() << endl;



	/********************************************************************************/
	int idSala4 = serv.pedirIdSala(1, Tipos::TP_ENTRADA_SALA, Tipos::LUGAR_ZOO);
	ids[idSala4] = serv.idBrokerConectado();
	cout << "id Sala 1(EZ) : " << idSala4 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;

	int idSala5 = serv.pedirIdSala(2, Tipos::TP_SALIDA, Tipos::LUGAR_ZOO);
	ids[idSala5] = serv.idBrokerConectado();
	cout << "id Sala 2(SZ) : " << idSala5 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;

	int idSala6 = serv.pedirIdSala(3, Tipos::TP_ENTRADA_SALA, Tipos::LUGAR_ZOO);
	ids[idSala6] = serv.idBrokerConectado();
	cout << "id Sala 3(EZ) : " << idSala6 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;


	/******************************************************************************/
	// levanto el broker caido
	int idBrokerLevantado = serv.pedirIdBroker(1, 999);
	ids[idBrokerLevantado] = serv.idBrokerConectado();
	cout << endl << "Broker relevantado, id: " << idBrokerLevantado << endl << endl;

	int idSala7 = serv.pedirIdSala(2, Tipos::TP_ENTRADA_SALA, Tipos::LUGAR_ZOO);
	ids[idSala7] = serv.idBrokerConectado();
	cout << "id Sala 2(EZ) : " << idSala7 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;

	int idSala8 = serv.pedirIdSala(3, Tipos::TP_SALIDA, Tipos::LUGAR_ENTRADA);
	ids[idSala8] = serv.idBrokerConectado();
	cout << "id Sala 3(SE) : " << idSala8 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;

	int idSala9 = serv.pedirIdSala(4, Tipos::TP_ENTRADA_SALA, Tipos::LUGAR_ZOO);
	ids[idSala9] = serv.idBrokerConectado();
	cout << "id Sala 4(EZ): " << idSala9 <<  " idBroker: "<< serv.idBrokerConectado() <<endl;



	int idRepuesto;
	int idBusReemp = serv.pedirIdBUSRepuestoGeneral(idRepuesto);
	cout << "id Bus Repuesto: " << idBusReemp << "idGral: "<< idRepuesto <<", idBroker: "<< serv.idBrokerConectado() <<endl;

	cout << "ID REPUESTO GENERAL: " << serv.idBusRepuestoGeneral() << endl;

	int idBusNuevo = serv.pedirIdBUSRepuesto(1, idBusReemp);
	ids[idBusNuevo] = serv.idBrokerConectado();
	cout << "id Bus Reemplazodo por roto: " << idBusNuevo << ", idBroker: "<< serv.idBrokerConectado() <<endl;

	id = serv.consultarIdSala(1, Tipos::TP_ENTRADA_COLA, Tipos::LUGAR_ENTRADA);

	if (id != idSala1) {
		errores++;
		cout << " Error en id de Sala 1" << endl;
	}


	id = serv.pedirIdSala(2, Tipos::TP_ENTRADA_COLA, Tipos::LUGAR_ZOO);
	ids[id] = serv.idBrokerConectado();

	if (id != idSala2) {
		errores++;
		cout << " Error en id de Sala 2" << endl;
	}


	id = serv.consultarIdSala(2, Tipos::TP_SALIDA, Tipos::LUGAR_ZOO);


	if (id != idSala5) {
		errores++;
		cout << " Error en id de Sala 5" << endl;
	}


	id = serv.consultarIdBus(1);

	if (id != idBus1) {
		errores++;
		cout << " Error en id de Bus 1 esp:"<< idBus1 << ", recb: "<< id << endl;
	}



	id = serv.consultarIdBus(2);

	if (id != idBus2) {
		errores++;
		cout << " Error en id de Bus 2 esp:"<< idBus2 << ", recb: "<< id << endl;
	}

	id = serv.pedirIdBus(2);

	if (id != idBus2) {
		errores++;
		cout << " Error en id de Bus 1 esp:"<< idBus2 << ", recb: "<< id << endl;
	}


//	serv.consultarDatos(idSala6);
//	cout << "COnsultados datos de idSala6: "<< idSala6 <<", idBroker:" << serv.idBrokerConectado() << endl;

	std::map<int,int>::iterator it;
	for (it = ids.begin() ; it != ids.end(); ++it) {
		serv.consultarDatos(it->first);
		if (serv.idBrokerConectado() != it->second) {
			errores++;

			cout << "Error en id :" << it->first << " con broker concectado a el. id Broker esperado: " << it->second;
			cout << ", id Broker recibido: " << serv.idBrokerConectado() << endl;

		}
	}


	cout << "Errores: "<< errores << endl;

	cout << "pid: " << getpid() << endl;


	return 0;
}
